package ga.fitness;

import ga.chromosome.Chromosome;
import ga.gene.TheException;

import java.util.ArrayList;

/**
 * The Class FitnessCalc.
 */
public class FitnessCalc {
	

	
	private static ArrayList<Boolean> output = new ArrayList<Boolean>();
	
	/**
	 * Gets the solution.
	 *
	 * @return the solution
	 */
	public static ArrayList<Boolean> getSolution(){
		return output;
	}
	
	/**
	 * Sets the solution.
	 *
	 * @param finalOutput the new solution
	 */
	public static void setSolution(ArrayList<Boolean> finalOutput){
		output = finalOutput;
	}
	
	/**
	 * Gets the fitness and sets the matches.
	 *
	 * @param chromo the chromo
	 * @return the fitness
	 * @throws TheException the the exception
	 */
	public static int getFitness(Chromosome chromo) throws TheException{
		ArrayList<ArrayList<Boolean>> solution = chromo.solve();
		boolean[][] boolArray = new boolean[solution.get(0).size()][solution.size()];
		for(int i = 0; i < solution.size(); i++){
			for(int j = 0; j < solution.get(i).size(); j++){
				boolArray[j][i] = solution.get(i).get(j);
				//2x7  2 output gates, 7 outputs per gate
			}
		}
		int matchCounter = 0;
		for(int i = 0; i < boolArray.length; i++){
			for(int j = 0; j < boolArray[i].length; j++){
				if(boolArray[i][j] == output.get(j))
					matchCounter++;
			}
			if(matchCounter == boolArray[i].length){
				chromo.setMatch(i);
				
			}
			matchCounter = 0;
		}
		return chromo.traceOutput();
	}
	



}
